<?php
require_once ROOT.'/app/user.php';

class gold extends page{
	
	private $op_gold = array();
	private $number = array();
	private $pay_type_taobao = 1;
	
	function __construct(){
		parent::__construct();
		
		$this->op_gold = array(
			'op_bookshelf' => -100,
			'op_search' => -20,
			'op_bookupdate' => -20,
			'op_vip_one' => -1500,
			'op_vip_three' => -4300,
			'op_vip_six' => -8500,				
			'op_vip_twelve' => -13500,				
			'op_sign' => 10,
			'op_recharge_five' => 500,
			'op_recharge_fifteen' => 1500,
			'op_recharge_fifty' => 5000,
			'op_recharge_hundred' => 10000,
			'op_recharge_2hundred' => 20000,
		);
		
		$this->number = array(
			'one' => 1,
			'three' => 3,
			'five' => 5,
			'six' => 6,
			'ten' => 10,
			'twelve' => 12,		
			'thirty' => 30,
			'fifteen' => 15,
			'fifty' => '50',
			'hundred' => 100,
			'2hundred' => 200,
		);
	}
	
	function op(){
		$device_id = $this->req['device_id'];
		$a = $this->req['a'];
		
		$json = array();
		if(!$device_id){
			$json['status'] = $this->f_status;
			$json['message'] = '缺少DeviceID';
			G::toClient($json);
		}
		
		$key = 'op_'.$a;
		if(!array_key_exists($key, $this->op_gold)){
			$json['status'] = $this->f_status;
			$json['message'] = 'Wrong way!';
			G::toClient($json);
		}
		
		$user = new user();
		$userInfo = $user->findByDeviceID($device_id);
		$gold = $userInfo['gold'];
		
		if(stripos($key, 'search')!==false || stripos($key, 'bookupdate')!==false){
			$s_key = 'public';
		}elseif(stripos($key, 'bookshelf')!==false){
			$s_key = 'bookshelf';
		}elseif(stripos($key, 'vip')!==false){
			$s_key = 'vip';
		}elseif(stripos($key, 'sign')!==false){
			$s_key = 'sign';
		}elseif(stripos($key, 'recharge')!==false){
			$s_key = 'recharge';
		}

		$c = $this->op_gold[$key];
                if(($c+$gold)<0 && $s_key!='vip'){
                        $json['status'] = $this->f_status;
                        $json['message'] = '钱不够';
                        G::toClient($json);
                }

		$op = $a;
		$gold = $c;
		$uid = $userInfo['uid'];
		switch ($s_key){
			case 'public':
				$return = $this->opPublic($op, $uid, $gold);
				break;
			case 'bookshelf':
				$return = $this->opBookShelf($op, $uid, $gold);
				break;
			case 'sign':
				$return = $this->opSign($op, $uid, $gold);
				break;
			case 'vip':
				$return = $this->opVip($op, $uid, $gold);
				break;
			case 'recharge':
				$return = $this->opRecharge($op, $uid);
				break;	
			default:
				$return = false;
				break;
		}
		
		if(!$return){
			$json['status']=$this->f_status;
			G::toClient($json);
		}
		$json['status']=$this->s_status;
		G::toClient($json);
	}

	public function opWall(){
		$gold = intval($this->req['g']);
		$device_id = $this->req['device_id'];

		$json = array();
                if(!$device_id){
                        $json['status'] = $this->f_status;
                        $json['message'] = '缺少DeviceID';
                        G::toClient($json);
                }

		if($gold>200){
			$json['status'] = $this->f_status;
                        $json['message'] = '无效积分';
                        G::toClient($json);
		}

		$user = new user();
                $userInfo = $user->findByDeviceID($device_id);
		$uid = $userInfo['uid'];
		
        	$this->insertOP('wall', $uid, $gold);
                $r=$this->updateGold($uid, $gold);

		if(!$r){
                        $json['status']=$this->f_status;
                        G::toClient($json);
                }
                $json['status']=$this->s_status;
                G::toClient($json);
	}

	public function opGoldVip(){
		$device_id = $this->req['device_id'];

		$json = array();
                if(!$device_id){
                        $json['status'] = $this->f_status;
                        $json['message'] = '缺少DeviceID';
                        G::toClient($json);
                }

		$user = new user();
                $userInfo = $user->findByDeviceID($device_id);
                $uid = $userInfo['uid'];
		$gold = $userInfo['gold'];

		$key = 'op_vip_one';
		$op = 'vip_one';

                $c = $this->op_gold[$key];
                if(($c+$gold)<0){
                        $json['status'] = $this->f_status;
                        $json['message'] = '钱不够';
                        G::toClient($json);
                }

		$return = $this->opVip($op, $uid, $c);	
		if($return){
			$this->updateGold($uid, $c);
			$json['status'] = $this->s_status;			
		}else{
			$json['status'] = $this->f_status;
		}		
		G::toClient($json);
	}
	
	/*******************************************************************************/
	
	private function opPublic($op, $uid, $gold){
		if($op!='search' && $op!='bookupdate'){
			return false;
		}
		
		$this->insertOP($op, $uid);
		return $this->updateGold($uid, $gold);
	}
	
	private function opBookShelf($op, $uid, $gold){
		if($op!='bookshelf')return false;
		
		$rs = $this->insertOP($op, $uid);
		if(!$rs){
			return false;
		}
		
		$update_sql = "update user set book_count = book_count+3, gold=gold+{$gold} where uid = '{$uid}' limit 1";
		$update_rs = $this->db->exec($update_sql);
		if(!$update_rs){
			return false;
		}
		return true;
	}
	
	private function opSign($op, $uid, $gold){
		if($op != 'sign')return false;
		
		$today = date('Y-m-d', time());
		
		$select_sql = "select count(*) as c from op where uid='{$uid}' and create_time like '%{$today}%' and type='sign' limit 1";
		$rs = $this->db->query($select_sql)->fetch();
		if($rs['c']){
			return false;
		}
		
		$this->insertOP($op, $uid);
		return $this->updateGold($uid, $gold);
	}
	
	private function opVip($op, $uid, $gold){
		if(stripos($op, 'vip')===false)return false;
		
		$l = explode('_', $op);
		if(!array_key_exists($l[1], $this->number)){
			return false;
		}
		$num = $this->number[$l[1]];
		
		$rs = $this->insertOP($op, $uid, $num);
		if(!$rs){
			return false;
		}
		
		$addDay = $num*30-1;
		$today = date('Y-m-d', time());
		$select_sql = "select uid, start_time, end_time, is_vip from vip where uid = '{$uid}' limit 1";
		$select_rs = $this->db->query($select_sql)->fetch();
		if(!$select_rs){
			$start = $today;
			$end = date('Y-m-d', strtotime("+{$addDay} day"));
			$is_vip = 1;
			
			$insert_vip_sql = "insert into vip (uid, start_time, end_time, is_vip, create_time) values ('{$uid}', '{$start}', '{$end}', '{$is_vip}', now())";
			$insert_vip_rs = $this->db->exec($insert_vip_sql);
		}else{
			if($select_rs['end_time'] <= $today){
				$start = $today;
				$end = date('Y-m-d', strtotime("+{$addDay} day"));
			}else{
				$start = $select_rs['start_time'];
				$end_day = strtotime($select_rs['end_time'])+$addDay*86400;
				$end = date('Y-m-d', $end_day);
			}
			$is_vip = 1;
			
			$update_vip_sql = "update vip set start_time='{$start}', end_time='{$end}', is_vip='{$is_vip}' where uid='{$uid}'";
			$update_vip_rs = $this->db->exec($update_vip_sql);
		}
		return true;//$this->updateGold($uid, $gold);
	}
	
	private function opRecharge($op, $uid){
		if(stripos($op, 'recharge')===false)return false;
		
		$l = 'op_'.$op;
		if(!array_key_exists($l, $this->op_gold)){
			return false;
		}
		$gold = $this->op_gold[$l];
		
		$insert_sql = "insert into payment (uid,gold,type,create_time) values ('{$uid}','{$gold}','{$this->pay_type_taobao}', now())";
		$insert_rs = $this->db->exec($insert_sql);
		if(!insert_rs){
			return false;
		}
		
		$update_sql = "update user set gold=gold+{$gold} where uid='$uid' limit 1";
		$update_rs = $this->db->exec($update_sql);
		if(!update_rs){
			return false;
		}
		return true;
	}
	
	private function insertOP($op, $uid, $num=0){
		$insert_sql = "insert into op (uid, type, num, create_time) values ('{$uid}', '{$op}', '{$num}', now())";
		$rs = $this->db->exec($insert_sql);
		if(!$rs){
			return false;
		}
		return true;
	}
	
	private function updateGold($uid, $gold){
		$update_sql = "update user set gold=gold+{$gold} where uid='{$uid}' limit 1";
		$rs = $this->db->exec($update_sql);
		if(!$rs){
			return false;
		}
		return true;
	}
}
